package com.jfinal.weixin.demo;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.kit.PropKit;
import com.jfinal.weixin.interceptor.JSTicketInterceptor;
import com.jfinal.weixin.sdk.api.SnsAccessToken;
import com.jfinal.weixin.sdk.api.SnsAccessTokenApi;
import com.jfinal.weixin.sdk.jfinal.OAuthterceptor;
import com.jfinal.weixin.sdk.jfinal.PathInterceptor;
public class OauthController extends Controller {
	@Before({OAuthterceptor.class,PathInterceptor.class})
  public void test()
  {
	  renderJsp("/jsp/test.jsp");
  }
	public void code()
	{
		String code=getPara("code");
		String state=getPara("state");
		if(code!=null&&!code.equals(""))
		{
			System.out.println(("code=:"+code+",state:"+state));
			SnsAccessToken token=SnsAccessTokenApi.getSnsAccessToken(PropKit.get("appId"), PropKit.get("appSecret"), code);
			renderText("openid:"+token.getOpenid());
			setSessionAttr("openid", token.getOpenid());
			String interceptUrl=getSessionAttr("interceptUrl");
			if(interceptUrl!=null)
			{
			removeSessionAttr("interceptUrl");
			redirect(interceptUrl);
			}
		}
	}
}
